package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {

    private void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面参数
        String username = req.getParameter("username");
        String pwd = req.getParameter("password");
        String remember = req.getParameter("remember");
        if ("zhaosi".equals(username) && "666".equals(pwd)) {
            Cookie cookieName = new Cookie("username", username);
            Cookie cookieName1 = new Cookie("password", pwd);
            /*System.out.println("remember = " + remember);*/
            if (remember == null) {
                //设置存活时间为0
                cookieName.setMaxAge(0);
                cookieName1.setMaxAge(0);
            } else {
                //设置存活时间  一周
                cookieName.setMaxAge(7 * 24 * 60 * 60);
                cookieName1.setMaxAge(7 * 24 * 60 * 60);
            }
            //设置Cookie存储路径:设置在根路径,将来页面获取就不需要考虑存储的路径;
            cookieName.setPath("/");
            cookieName1.setPath("/");
            resp.addCookie(cookieName);
            resp.addCookie(cookieName1);
            resp.sendRedirect(req.getContextPath() + "/page/LoginServlet.jsp");
        } else {
            resp.sendRedirect(req.getContextPath() + "/page/login.jsp");
        }
    }
}
